import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './modules/auth.module';
import { UsersModule } from './modules/users.module';
// 配置环境
import { ConfigModule } from '@nestjs/config';
// 权限接口
import { JwtAuthGuard } from './guards/jwt-auth.guard';
// 注册全局守卫
import { APP_GUARD } from '@nestjs/core';
// API 模块
import { ApiModule } from './api/api.module';
// 通用模块
import { CommonModule } from './common/common.module';

export const IS_DEV = process.env.RUNNING_ENV === 'dev';
let envFilePath = ['.env'];
if (IS_DEV) {
  envFilePath.unshift('.env.dev');
} else {
  envFilePath.unshift('.env.prod');
}

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 全局配置
      envFilePath, // 优先级递减，不是覆盖
    }),
    AuthModule,
    UsersModule,
    ApiModule,
    CommonModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
  ],
})
export class AppModule {}
